package com.hgyc.mom.common.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hgyc.mom.common.enums.CodeMsg;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;

import java.io.Serializable;

/**
 *   接口返回数据格式
 *
 * @author fwj
 * @since 2023-11-13
 */
@Data
@Tag(name="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	@Schema(name = "成功标志")
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	@Schema(name = "返回处理消息")
	private String message = "";

	/**
	 * 返回代码
	 */
	@Schema(name = "返回代码")
	private Integer code = 0;
	
	/**
	 * 返回数据对象 data
	 */
	@Schema(name = "返回数据对象")
	private T data;
	
	/**
	 * 时间戳
	 */
	@Schema(name = "时间戳")
	private long timestamp = System.currentTimeMillis();

	public Result() {
	}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param message
     */
	public Result(Integer code, String message) {
		this.code = code;
		this.message = message;
	}

	public static<T> Result<T> success() {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(CodeMsg.SUCCESS.getCode());
		return r;
	}

	public static<T> Result<T> success(String msg) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(CodeMsg.SUCCESS.getCode());
		r.setMessage(msg);
		return r;
	}

	public static<T> Result<T> success(String msg, T data) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(CodeMsg.SUCCESS.getCode());
		r.setMessage(msg);
		r.setData(data);
		return r;
	}

	public static<T> Result<T> success(T data) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(CodeMsg.SUCCESS.getCode());
		r.setData(data);
		return r;
	}

	public static<T> Result<T> error(String msg, T data) {
		Result<T> r = new Result<T>();
		r.setSuccess(false);
		r.setCode(CodeMsg.ERROR.getCode());
		r.setMessage(msg);
		r.setData(data);
		return r;
	}

	public static<T> Result<T> error(CodeMsg code) {
		Result<T> r = new Result<T>();
		r.setCode(code.getCode());
		r.setMessage(code.getMsg());
		r.setSuccess(false);
		return r;
	}

	public static<T> Result<T> error(String message) {
		Result<T> r = new Result<T>();
		r.setCode(CodeMsg.ERROR.getCode());
		r.setMessage(message);
		r.setSuccess(false);
		return r;
	}

	/**
	 * 无权限访问返回结果
	 */
	public static<T> Result<T> noauth(String msg) {
		return error(CodeMsg.LOGIN_ERROR);
	}

	@JsonIgnore
	private String onlTable;

}